<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="渠道上报的请求流水ID" prop="requestId">
          <el-input v-model="dataForm.requestId" placeholder="渠道上报的请求流水ID"></el-input>
      </el-form-item>
          <el-form-item label="事件类型（imp曝光，click点击,download下载）" prop="eventType">
          <el-input v-model="dataForm.eventType" placeholder="事件类型（imp曝光，click点击,download下载）"></el-input>
      </el-form-item>
          <el-form-item label="事件发生的时间" prop="traceTime">
          <el-input v-model="dataForm.traceTime" placeholder="事件发生的时间"></el-input>
      </el-form-item>
          <el-form-item label="上报的IP" prop="ip">
          <el-input v-model="dataForm.ip" placeholder="上报的IP"></el-input>
      </el-form-item>
          <el-form-item label="设备标识" prop="oaid">
          <el-input v-model="dataForm.oaid" placeholder="设备标识"></el-input>
      </el-form-item>
          <el-form-item label="计划ID" prop="campaignId">
          <el-input v-model="dataForm.campaignId" placeholder="计划ID"></el-input>
      </el-form-item>
          <el-form-item label="任务ID" prop="adgroupId">
          <el-input v-model="dataForm.adgroupId" placeholder="任务ID"></el-input>
      </el-form-item>
          <el-form-item label="素材ID" prop="contentId">
          <el-input v-model="dataForm.contentId" placeholder="素材ID"></el-input>
      </el-form-item>
         
          
          <el-form-item label="转换类型" prop="resultType">
          <el-input v-model="dataForm.resultType" placeholder="转换类型"></el-input>
      </el-form-item>
          <el-form-item label="转化时间" prop="resultTime">
          <el-input v-model="dataForm.resultTime" placeholder="转化时间"></el-input>
      </el-form-item>
          <el-form-item label="自己的流水ID" prop="bizRequestId">
          <el-input v-model="dataForm.bizRequestId" placeholder="自己的流水ID"></el-input>
      </el-form-item>
          <el-form-item label="仅支持
" prop="imei">
          <el-input v-model="dataForm.imei" placeholder="仅支持
非华为手机且采集不到oaid的场景"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
          <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        requestId: '',
        eventType: '',
        traceTime: '',
        ip: '',
        oaid: '',
        campaignId: '',
        adgroupId: '',
        contentId: '',
        callback: '',
        channel: '',
        tagId: '',
        resultType: '',
        resultTime: '',
        bizRequestId: '',
        imei: '',
        createTime: '',
        updateTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        requestId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        eventType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        traceTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ip: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        oaid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        campaignId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        adgroupId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contentId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        callback: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        channel: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        tagId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        resultType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        resultTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        bizRequestId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        imei: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/adx/report/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/adx/report/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
